var commonUtil = require("../../../utils/commonUtil.js")
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
var gcoord = require("../../../utils/gcoord.js")
Page({

  data: {
    Height: 0,
    scale: 13,
    latitude: "",//必须有当前经纬度才能展示地图
    longitude: "",
    markers: [],
    controls: [{
      id: 1,
      iconPath: "../../../image/common/jianhao.png",
      position: {
        left: 280,
        top: 100 - 50,
        width: 30,
        height: 25
      },
      clickable: true
    },
    {
      id: 2,
      iconPath: "../../../image/common/jiahao.png",
      position: {
        left: 320,
        top: 100 - 50,
        width: 30,
        height: 25
      },
      clickable: true
    }],
    circles: [],
    imgUrl: imgUrl,
    startDate: "",
    endDate: "",
  },
  startDatecg: function (e) {
    let time = e.detail.value;
    this.setData({ startDate: time })
  },
  endDatecg: function (e) {
    let time = e.detail.value;
    this.setData({ endDate: time })
  },
  loadData: function () {
    if (commonUtil.isEmpty(this.data.startDate) || commonUtil.isEmpty(this.data.endDate)) {
      commonUtil.showModal("请选择日期！");
      return;
    }
    var that = this;
    var points = [];//经纬度数组
    let params = new Object();
    params.plate = wx.getStorageSync("aidKey");
    params.begTime = this.data.startDate + " 00:00:00";
    params.endTime = this.data.endDate + " 23:59:59";
    // params.begTime = "2018-10-20 00:00:00";
    // params.endTime = "2018-10-22 23:59:59";
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/carinfo/getTrajectoryByPlateAndDate",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        //console.log("res：" + JSON.stringify(res));
        if (res.code == 0) {
          let list = JSON.parse(res.data);
          if (list != null && list.length > 0) {
            for (let i = 0; i < list.length; i++) {
              var gc = gcoord.transform([list[i].lon, list[i].lat], gcoord.WGS84, gcoord.GCJ02);//转换格式
              var row = new Object();
              row.latitude = gc[1];
              row.longitude = gc[0];
              points.push(row);
            }
            that.setData({
              latitude: points[0].latitude,
              longitude: points[0].longitude,
              polyline: [{
                points: points,//经纬度数组
                color: "#E62327",
                width: 4,
                dottedLine: false,//true为虚线
                arrowLine:true,
              }],
              markers: [{
                id: "1",
                latitude: points[0].latitude,
                longitude: points[0].longitude,
                width: 40,
                height: 50,
                iconPath: "../../../image/common/qidian.png",
                title: "起始位置"
              },
              {
                id: "2",
                latitude: points[points.length - 1].latitude,
                longitude: points[points.length - 1].longitude,
                width: 40,
                height: 50,
                iconPath: "../../../image/common/zhongdian.png",
                title: "最后位置"
              }],
            })
          }
        } else {
          wx.showModal({ title: '提示', content: res.message, })
        }
      }
    })
  },
  //点击地图事件
  clickMap: function (e) {
    console.log(JSON.stringify(e));
  },
  //视野发生变化时触发
  regionchange(e) {
    console.log("regionchange===" + e.type)
  },
  //点击merkers
  markertap(e) {
    console.log(e.markerId)
  },
  //点击缩放按钮动态请求数据
  controltap(e) {
    var that = this;
    console.log("scale===" + this.data.scale)
    if (e.controlId === 1) {
      that.setData({
        scale: --this.data.scale
      })
    } else {
      that.setData({
        scale: ++this.data.scale
      })
    }
  },
  onLoad: function (options) {
    // this.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})